package com.qida.clm.core.network;

import com.android.volley.NetworkError;
import com.android.volley.NetworkResponse;
import com.android.volley.NoConnectionError;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.qida.clm.core.utils.LogUtil;

/**
 * Created by edgar on 2015/12/19.
 */
public class MessageCode {
	/**
	 * 当请求服务器成功,但是返回的数据不正常导致数据解析异常
	 */
	public static final int FAIL = -1;
	/**
	 * 服务器返回的数据有errorCode和errorMsg
	 */
	public static final int SERVER_ERROR_FAIL = 1;
	/**
	 * 请求服务器成功,并且数据解析正常.
	 */
	public static final int SUCCEED = 0;
	/**
	 * 连接超时
	 */
	public static final int TIME_OUT = 1000;
	/**
	 * 网络异常
	 */
	public static final int NETWORK_ERROR = 1001;
	/**
	 * 服务器异常
	 */
	public static final int SERVER_ERROR = 1002;
	/**
	 * 没有连接
	 */
	public static final int NO_CONNECTION_ERROR = 1003;
	/**
	 * 未知错误
	 */
	public static final int UNKNOWN_ERROR = 1004;

	public static ResponseException getResponseErrorByVolleyError(VolleyError e) {
		String errorMsg;
		int errorCode;
		NetworkResponse networkResponse = e.networkResponse;
		if (networkResponse != null && networkResponse.data != null) {
			errorMsg = new String(networkResponse.data);
		} else {
			errorMsg = e.getMessage();
		}
		LogUtil.e(MessageCode.class.getSimpleName(), "errorMsg:" + errorMsg);
		if (e instanceof TimeoutError) {
			errorCode = TIME_OUT;
			errorMsg = "连接超时";
		} else if (e instanceof NetworkError) {
			errorCode = NETWORK_ERROR;
			errorMsg = "网络异常";
		} else if (e instanceof ServerError) {
			errorCode = SERVER_ERROR;
			errorMsg = "服务器异常";
		} else if (e instanceof NoConnectionError) {
			errorCode = NO_CONNECTION_ERROR;
			errorMsg = "没有连接";
		} else {
			errorCode = UNKNOWN_ERROR;
			errorMsg = "未知错误";
		}
		return new ResponseException(errorCode, errorMsg);
	}
}